package com.frejoys.common.util;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class CacheUtil {

    public final RedisUtil redisUtil;

    private static CacheUtil cacheUtil;

    @PostConstruct
    private void init() {
        cacheUtil = this;
    }

    /**
     * 添加订单到超时集合
     * @param orderSn
     * @param time
     */
    public static void addOrderTimeoutSortSet(Long orderSn, Integer time) {
        cacheUtil.redisUtil.zAdd(CacheKeyUtil.orderTimeoutSortedSet(), orderSn, time);
    }

    /**
     * 移除订单超时集合中的订单
     * @param orderSn
     * @return
     */
    public static Long delOrderTimeoutSortSet(Long orderSn) {
        return cacheUtil.redisUtil.zRemove(CacheKeyUtil.orderTimeoutSortedSet(), orderSn);
    }

    /**
     * 获取订单超时集合信息
     * @param time
     * @param offset
     * @param count
     * @return
     */
    public static Set<String> getOrderTimeoutSortSet(Integer time, long offset, long count) {
        return cacheUtil.redisUtil.zRangeByScore(CacheKeyUtil.orderTimeoutSortedSet(), 0, time, offset, count);
    }

}
